package com.personal.service.Impl;

import com.personal.bo.ShopCartBO;
import com.personal.enums.ResourcesEnum;
import com.personal.enums.ShopCartEnum;
import com.personal.enums.UserEnum;
import com.personal.exception.ExceptionUtils;
import com.personal.pojo.Resources;
import com.personal.service.ResourcesService;
import com.personal.service.ShopCartService;
import com.personal.utils.RedisUtils;
import com.personal.utils.Result;
import com.personal.vo.ShopCartVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private ResourcesService resourcesService;

    /**
     * 添加购物车
     * @param shopCartBO
     * @return
     */
    @Override
    public Result addCart(ShopCartBO shopCartBO) {
        log.info("添加购物车接口");
        //todo 这里添加购物车的时候得先判断用户是否是vip用户
        
        // 判断参数问题
        if (shopCartBO.getUserId() == null || shopCartBO.getUserId() <= 0){
            ExceptionUtils.error(UserEnum.USER_ID_IS_ERROR);
        }
        if (shopCartBO.getResourcesId() == null || shopCartBO.getResourcesId() <= 0){
            ExceptionUtils.error(ResourcesEnum.RESOURCES_ID_IS_ERROR);
        }

        // 判断购物车是否存在该商品
        ShopCartBO shopCartBO1 = (ShopCartBO) redisUtils.hget(shopCartBO.getUserId().toString(),shopCartBO.getResourcesId().toString());
        if (shopCartBO1 != null) {
            ExceptionUtils.error(ShopCartEnum.SHOP_CART_IS_EXIST);
        }
        //添加进购物车
        boolean flag = redisUtils.hset(shopCartBO.getUserId().toString(),shopCartBO.getResourcesId().toString(),shopCartBO);
        if (!flag) {
            ExceptionUtils.error(ShopCartEnum.SHOP_CART_IS_ERROR);
        }

        return Result.success();
    }

    @Override
    public Result getUserShopCartList(Integer userId) {
        log.info("查询我的购物车");
        // 判断参数问题
        if (userId == null || userId <= 0){
            ExceptionUtils.error(UserEnum.USER_ID_IS_ERROR);
        }
        ShopCartBO shopCartBO;
        //从redis中根据用户id取出所有数据
        Map<Object, Object> userShopCartList = redisUtils.hgetall(userId.toString());
        System.out.println("=================================================================\n"+userShopCartList);
        if (userShopCartList.isEmpty()) {
            ExceptionUtils.error(ShopCartEnum.SHOP_CART_is_null);
        }
        List<ShopCartVO> shopCartVOS = new ArrayList<>();
        for (Object value : userShopCartList.values()) {
            ShopCartVO shopCartVO = new ShopCartVO();
            shopCartBO = (ShopCartBO) value;

            BeanUtils.copyProperties(shopCartBO, shopCartVO);
            shopCartVOS.add(shopCartVO);
        }
        return Result.success(shopCartVOS);
    }

    /**
     * 清空购物车
     * @param userId
     * @return
     */
    @Override
    public Result clearUserShopCartList(Integer userId) {
        log.info("清空我的购物车");

        // 判断参数问题
        if (userId == null || userId <= 0){
            ExceptionUtils.error(UserEnum.USER_ID_IS_ERROR);
        }
        ShopCartBO shopCartBO;
        //从redis中根据用户id取出所有数据
        Map<Object, Object> userShopCartList = redisUtils.hgetall(userId.toString());
        System.out.println("=================================================================\n"+userShopCartList);
        if (userShopCartList.isEmpty()) {
            ExceptionUtils.error(ShopCartEnum.SHOP_CART_is_null);
        }

        for (Object value : userShopCartList.values()) {
            ShopCartVO shopCartVO = new ShopCartVO();
            shopCartBO = (ShopCartBO) value;

            redisUtils.hdel(userId.toString(),shopCartBO.getResourcesId().toString());
        }

        return Result.success();
    }

    /**
     * 通过id删除购物车中的商品
     * @param resourcesId
     * @return
     */
    @Override
    public Result deleteShopCartByResourcesId(Integer resourcesId,Integer userId) {
        log.info("删除购物车中的商品");
        // 判断参数问题
        if (resourcesId == null || resourcesId <= 0) {
            ExceptionUtils.error(ResourcesEnum.RESOURCES_ID_IS_ERROR);
        }
        if (userId == null || userId <= 0){
            ExceptionUtils.error(UserEnum.USER_ID_IS_ERROR);
        }
        //todo 本项目中的使用到userId的很多接口，后面都需要修改成token去校验
        redisUtils.hdel(userId.toString(),resourcesId.toString());

        return Result.success();
    }
}
